#include <stdlib.h>
#include <GL/glut.h>
#include "Grid.h"
#include <iostream>

Grid::Grid()
: _total(10)
{
	m_bDisplayGrid = true;
	
	// Default colors
	redCol = 0.9f;
	greenCol = 0.65f;
	blueCol = 0.65f;
}


void Grid::render(int ventana) const
{
	if(!m_bDisplayGrid) return;

	const float sep = ( (float)ventana)/((float) _total); // Distance between lines
	glColor3f(redCol, greenCol, blueCol);
	glBegin(GL_LINES);

	for( unsigned int i = 0; i < _total; ++i)
	{
		// horizontal
		glVertex2f(-ventana/2.0, i*sep-ventana/2.0);
		glVertex2f(ventana/2.0, i*sep-ventana/2.0);
		// vertical
		glVertex2f(i*sep-ventana/2.0,-ventana/2.0);
		glVertex2f(i*sep-ventana/2.0, ventana/2.0);
	}
	glEnd();
}

void Grid::renderTrajectory(int ventana) const
{
	if(!m_bDisplayGrid) return;

	const float sep = ( (float)ventana)/((float) _total); // Distance between lines
	glColor3f(redCol, greenCol, blueCol);
	glBegin(GL_LINES);

	for( unsigned int i = 0; i < _total; ++i)
	{
		// horizontal
		glVertex2f(-ventana/2.0, i*sep);
		glVertex2f(ventana/2.0, i*sep);
		// vertical
		glVertex2f(i*sep-ventana/2.0,0.0);
		glVertex2f(i*sep-ventana/2.0,ventana);
	}
	glEnd();
}

void Grid::show()
{
	m_bDisplayGrid = true;
}

void Grid::hide()
{
	m_bDisplayGrid = false;
}

void Grid::toggle()
{
	m_bDisplayGrid = !m_bDisplayGrid;
}

void Grid::setColor(float red, float green, float blue)
{
	redCol = red;
	greenCol = green;
	blueCol = blue;
}


void Grid::AddLine()
{
	_total++;
}

void Grid::RemoveLine()
{
	if(_total > 1)
	{
		_total--;
	}
}

/*void Grid::ClosestVertexTrajectory(float x, float y, float& xDevuelto, float& yDevuelto, int ventana){
	const float sep = ( (float)ventana)/((float) _total); // Distance between lines
	float distMin=9999999;
	
	for( unsigned int i = 0; i < _total; ++i)
	{
		// horizontal
		if( ((-ventana/2.0)*(-ventana/2.0) - x*x) + ((i*sep)*(i*sep) - y*y) < distMin){
			distMin= ((-ventana/2.0)*(-ventana/2.0) - x*x) + ((i*sep)*(i*sep) - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}

		if( ((ventana/2.0)*(ventana/2.0) - x*x) + ((i*sep)*(i*sep) - y*y) < distMin){
			distMin= ((ventana/2.0)*(ventana/2.0) - x*x) + ((i*sep)*(i*sep) - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}

		if( ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + (0.0 - y*y) < distMin){
			distMin= ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + (0.0 - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}

		if( ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + (ventana - y*y) < distMin){
			distMin= ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + (ventana - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}
	}
}*/

/*void Grid::ClosestVertexShape(float x, float y, float& xDevuelto, float& yDevuelto, int ventana){
	const float sep = ( (float)ventana)/((float) _total); // Distance between lines
	float distMin=9999999;
	
	for( unsigned int i = 0; i < _total; ++i)
	{
		// horizontal
		if( ((-ventana/2.0)*(-ventana/2.0) - x*x) + ((i*sep-ventana/2.0)*(i*sep-ventana/2.0i*sep) - y*y) < distMin){
			distMin= ((-ventana/2.0)*(-ventana/2.0) - x*x) + ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}

		if( ((ventana/2.0)*(ventana/2.0) - x*x) + ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - y*y) < distMin){
			distMin= ((ventana/2.0)*(ventana/2.0) - x*x) + ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}

		if( ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + ((-ventana/2.0)*(-ventana/2.0) - y*y) < distMin){
			distMin= ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + ((-ventana/2.0)*(-ventana/2.0) - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}

		if( ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + ((ventana/2.0)*(ventana/2.0) - y*y) < distMin){
			distMin= ((i*sep-ventana/2.0)*(i*sep-ventana/2.0) - x*x) + ((ventana/2.0)*(ventana/2.0) - y*y);
			xDevuelto=x;
			yDevuelto=y;
		}
	}
}*/
